REM Copyright (c) 2011, http://code.google.com/p/example-xplane-plugins/
REM All rights reserved.
REM 
REM Redistribution and use in source and binary forms, with or without
REM modification, are permitted provided that the following conditions are met:
REM     * Redistributions of source code must retain the above copyright
REM       notice, this list of conditions and the following disclaimer.
REM     * Redistributions in binary form must reproduce the above copyright
REM       notice, this list of conditions and the following disclaimer in the
REM       documentation and/or other materials provided with the distribution.
REM     * Neither the name of "example-xplane-plugins" nor the
REM       names of its contributors may be used to endorse or promote products
REM       derived from this software without specific prior written permission.
REM 
REM THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
REM ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
REM WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
REM DISCLAIMED. IN NO EVENT SHALL "example-xplane-plugins" BE LIABLE FOR ANY
REM DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
REM (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
REM LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
REM ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
REM (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
REM SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



REM The environment variable XPLANE_PLUGINS_DIR should point to the XPlane plugins directory.
REM EXAMPLE: "C:\XPlane\Resources\plugins\" without the quotes.

REM Visual studio post build event:
REM     CopyPluginToXPlane.bat $(TargetPath) $(ProjectName)



ECHO ---------------------------------------------
ECHO COPYING DLL TO THE XPLANE PLUGIN DIRECTORY...
ECHO ---------------------------------------------

ECHO Checking for XPlane Plugin Environment Variable... (XPLANE_PLUGINS_DIR)
IF (%XPLANE_PLUGINS_DIR%)==() GOTO EnvNotFound
  ECHO PASS: Exists!

REM Removing extra quotes that may exist
SET XPLANE_PLUGINS_DIR=%XPLANE_PLUGINS_DIR:"=%

SET SOURCE_FILE=%~1
SET PLUGIN_NAME=%~2

SET DEST_DIR=%XPLANE_PLUGINS_DIR%\%PLUGIN_NAME%
SET DEST_FILE=%DEST_DIR%\win.xpl


ECHO Checking for existence of XPlane Plugin directory... "%XPLANE_PLUGINS_DIR%"
IF NOT EXIST "%XPLANE_PLUGINS_DIR%" GOTO PluginDirNotFound
  ECHO PASS: Exists!

ECHO Got 'plugin subfolder name' "%PLUGIN_NAME%" (Second param with no path)
IF EXIST "%DEST_DIR%" GOTO DirCreated
  ECHO Making plugin directory... "%DEST_DIR%"
  MD "%DEST_DIR%"
  IF NOT ERRORLEVEL 0 GOTO MakeDirError
    ECHO PASS: Created new directory for this plugin!

:DirCreated
ECHO Checking for existence of destination folder "%DEST_DIR%"
IF NOT EXIST "%DEST_DIR%" GOTO PluginSubDirNotFound
  ECHO PASS: Exists!

ECHO Checking for existence of 'target filename' "%SOURCE_FILE%" (First param with full path)
IF NOT EXIST "%SOURCE_FILE%" GOTO SourceFileNotFound
  ECHO PASS: Exists!

ECHO Copying "%SOURCE_FILE%" to "%DEST_FILE%"
IF EXIST "%DEST_FILE%" (

  REM Just overwrite existing file without prompt.
  XCOPY /Y "%SOURCE_FILE%" "%DEST_FILE%"

) ELSE (

  REM File does not already exist.
  REM Need to send in an F character for the imfamous XCOPY 'File or Directory' question that appears.
  ECHO F|XCOPY /Y "%SOURCE_FILE%" "%DEST_FILE%"

)

IF NOT ERRORLEVEL 0 GOTO CopyError
  ECHO PASS: No errors while copying!



:Complete
ECHO ---------------------------------------------
ECHO SUCCESS: Plugin file copied to X-Plane plugin directory!
ECHO ---------------------------------------------
EXIT /B 0

:EnvNotFound
ECHO ---------------------------------------------
ECHO ERROR: The Environment Variable (XPLANE_PLUGINS_DIR) is NOT defined!
ECHO ---------------------------------------------
EXIT /B 1

:PluginDirNotFound
ECHO ---------------------------------------------
ECHO ERROR: XPlane plugin directory does not exist or is empty!
ECHO Check the Environment Variable (XPLANE_PLUGINS_DIR) contains your "XPlane\Resources\plugins\" directory!
ECHO ---------------------------------------------
EXIT /B 2

:MakeDirError
ECHO ---------------------------------------------
ECHO ERROR: Make Directory command (MD) went wrong!
ECHO ---------------------------------------------
EXIT /B 3

:PluginSubDirNotFound
ECHO ---------------------------------------------
ECHO ERROR: Project plugin folder not found in the XPlane plug in folder
ECHO Check the XPlane plugin folder for your project folder.  E.g. "XPlane\Resources\plugins\MyOwnPlugin\"
ECHO ---------------------------------------------
EXIT /B 4

:SourceFileNotFound
ECHO ---------------------------------------------
ECHO ERROR: Source file not found (Compiled DLL)
ECHO Ensure compilation was successful.
ECHO ---------------------------------------------
EXIT /B 5

:CopyError
ECHO ---------------------------------------------
ECHO ERROR: Copy command (XCOPY) went wrong!
ECHO ---------------------------------------------
EXIT /B 6